package com.kunka.utils;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * Spring容器工厂工具类
 *
 *
 * 中间件不便于调用dubbo的服务，这边进行获取spring的bean
 * 将dubbo转化后的bean 进行获取
 *
 */
@Component
public class SpringBeanFactoryUtils implements ApplicationContextAware {

    private static ApplicationContext context = null;

     public static <T> T getBean(String name, Class<T> type) {
        return context.getBean(name, type);
     }


    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (SpringBeanFactoryUtils.context == null) {
                SpringBeanFactoryUtils.context = applicationContext;
        }
    }

    //获取httpservletrequest 对象
    public static HttpServletRequest getRequest(){

        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }




}
